package triangleWorm;

import java.util.ArrayList;

import processing.core.PApplet;
import processing.core.PVector;

public class TriangleWorm implements IWorm{
	//Parent
	PApplet p;
	
	//Worm movement
	IWormMovement m;
	
	//Worm 'parts'	
	int elementNum = 20;	
	WormElement[] elements = new WormElement[elementNum];
	ArrayList<WormElement> dyingElements = new ArrayList<WormElement>();
	
	PVector lastPoint;
	float elementMaxDist = 20f;
	float elementMinDist = elementMaxDist / 5;
	float elementChangeDist;

	float wormLenth = elementNum * elementMaxDist;
	
	//Colors
	float averageColorIntensity = 175;
	float sumOfColorIntensity = averageColorIntensity * 3;
	
	public TriangleWorm(){
		//Initiate parts
		for(int i = 0; i < elementNum; i ++) {
			elements[i] = new WormElement();
		}
	}

	public void init(PApplet processing, IWormMovement movement) {
		//Get parent
		p = processing;
		
		//Get movement
		m = movement;
		
		//
		lastPoint = new PVector(m.getPosition().x, m.getPosition().y, m.getPosition().z);
		
		//Setup parts
		for(int i = 0; i < elements.length; i ++) {		
			PVector[] points = new PVector[3];
			
			for(int j = 0; j < points.length; j ++) {
				points[j] = new PVector(m.getPosition().x, m.getPosition().y, m.getPosition().z);
			}
			
			elements[i].init(p);
			elements[i].set(points, makeColor(), 0f);
		}	
		
		elementChangeDist = p.random(elementMinDist, elementMaxDist);
		
	}

	public void update() {
		//Update movement
		m.update();
		
		//If distance from first element if too big
		if(m.getPosition().dist(lastPoint) > elementChangeDist) {
			WormElement dyingElement = new WormElement();
			dyingElement.init(p);
			dyingElement.set(elements[elements.length - 1]);
			dyingElement.kill();
			dyingElements.add(dyingElement);
			
			for(int i = elements.length - 1; i > 0; i --) {
				elements[i].set(elements[i - 1]);
			};
			
			PVector[] closestPoints = elements[1].getTwoClosesPoints(m.getPosition());
			
			float randomShift = 20;
			PVector point = new PVector(m.getPosition().x + p.random(- randomShift, randomShift), 
					m.getPosition().y + p.random(- randomShift, randomShift), 
					m.getPosition().z + p.random(- randomShift, randomShift));
			elements[0].set(point, closestPoints[0], closestPoints[1], makeColor(), 0f);
			
			lastPoint.set(m.getPosition());
			elementChangeDist = p.random(elementMinDist, elementMaxDist);
		}
	}

	public void draw() {
		//Iterate over all elements
		for(int i = 1; i < elementNum; i ++) {
			//Draw
			elements[i].draw();
		}
		
		for (WormElement element: dyingElements) {
			if(element.isKilledToDeath()) {
				element = null;
			} else {
				element.draw();
			}			
		}
	}
	
	private int makeColor() {
		//Setup color
		float colorPart1 = sumOfColorIntensity;
		float colorPart2 = sumOfColorIntensity;
		//Get random intervals
		while(colorPart1 >= colorPart2 || colorPart1 > 255f || colorPart2 - colorPart1 > 255f || sumOfColorIntensity - colorPart2 > 255f) {
			colorPart1 = p.random(sumOfColorIntensity);
			colorPart2 = p.random(sumOfColorIntensity);
		}
		//
		float red = colorPart1;
		float green = colorPart2 - colorPart1;
		float blue = sumOfColorIntensity - colorPart2;
		
		return p.color(red, green, blue);
	}
	

	public PVector getPosition() {
		return m.getPosition();
	}
	
	public float getLength() {
		return wormLenth;
	}
	

}